package com.zx.framework.plat.role.controller;

import com.zx.framework.plat.role.po.Role;
import com.zx.framework.plat.role.service.RoleService;
import com.zx.framework.web.controller.BaseController;
import com.zx.framework.web.exception.AjaxException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(RoleController.class);
    @Resource
    private RoleService roleService;

    /**
     * 获取所有角色的json
     *
     * @return
     */
    @RequestMapping("/query/all")
    @ResponseBody
    public Object queryAll() {
        try {
            List<Role> roles = roleService.findAll("layer");
            return clientMap().success().message("获取成功！").datas(roles).getMap();
        } catch (Exception e) {
            LOGGER.error("", e);
            throw new AjaxException(e.getMessage());
        }
    }

    /**
     * 删除角色
     *
     * @return
     */
    @RequestMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") String id) {
        try {
            Role role = roleService.findRoleById(id);
            if (null!=role){
                roleService.removeRole(role);
            }else {
                clientMap().failure().message("删除失败,你所操作的数据不存在!").getMap();
            }
            return clientMap().success().message("删除成功!").getMap();
        } catch (Exception e) {
            LOGGER.error("删除角色失败!", e);
            throw new AjaxException(e.getMessage());
        }
    }
}
